/**
 * 本地存储工具类
 * 支持Cookie、LocalStorage、SessionStorage操作
 */
class Storage {
  /**
   * 设置Cookie
   * @param {string} name - cookie名称
   * @param {string|object} value - cookie值，支持对象自动序列化
   * @param {number} days - 过期天数（可选，默认会话cookie）
   * @param {object} options - 其他参数 {path, domain, secure, httponly, samesite}
   */
  static setCookie(name, value, days = null, options = {}) {
    // 处理对象值
    const stringValue = typeof value === 'object' ? JSON.stringify(value) : value;
    const encodedValue = encodeURIComponent(stringValue);
    
    let cookieString = `${name}=${encodedValue}`;
    
    // 设置过期时间
    if (days !== null) {
      const expires = new Date(Date.now() + days * 86400000).toUTCString();
      cookieString += `; expires=${expires}`;
    }
    
    // 设置其他参数
    if (options.path) cookieString += `; path=${options.path}`;
    if (options.domain) cookieString += `; domain=${options.domain}`;
    if (options.secure) cookieString += '; secure';
    if (options.httponly) cookieString += '; httponly';
    if (options.samesite) cookieString += `; samesite=${options.samesite}`;
    
    document.cookie = cookieString;
  }
  
  /**
   * 获取Cookie
   * @param {string} name - cookie名称
   * @param {boolean} parseJSON - 是否解析JSON对象
   */
  static getCookie(name, parseJSON = false) {
    const value = `; ${document.cookie}`;
    const parts = value.split(`; ${name}=`);
    
    if (parts.length === 2) {
      const cookieValue = decodeURIComponent(parts.pop().split(';')[0]);
      
      if (parseJSON) {
        try {
          return JSON.parse(cookieValue);
        } catch {
          return cookieValue;
        }
      }
      return cookieValue;
    }
    return null;
  }
  
  /**
   * 删除Cookie
   * @param {string} name - cookie名称
   * @param {string} path - 路径（需与设置时一致）
   */
  static removeCookie(name, path = '/') {
    document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${path}`;
  }
  
  /**
   * LocalStorage操作（带JSON序列化）
   */
  static setLocal(key, value) {
    try {
      localStorage.setItem(key, JSON.stringify(value));
    } catch (e) {
      console.error('LocalStorage设置失败:', e);
    }
  }
  
  static getLocal(key) {
    try {
      const item = localStorage.getItem(key);
      return item ? JSON.parse(item) : null;
    } catch {
      return null;
    }
  }
  
  static removeLocal(key) {
    localStorage.removeItem(key);
  }
  
  /**
   * SessionStorage操作
   */
  static setSession(key, value) {
    try {
      sessionStorage.setItem(key, JSON.stringify(value));
    } catch (e) {
      console.error('SessionStorage设置失败:', e);
    }
  }
  
  static getSession(key) {
    try {
      const item = sessionStorage.getItem(key);
      return item ? JSON.parse(item) : null;
    } catch {
      return null;
    }
  }
  
  static removeSession(key) {
    sessionStorage.removeItem(key);
  }
}

export default Storage;